/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package cn.kcrx.autoweightapp.fragment

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.withStateAtLeast
import androidx.room.Room
import cn.kcrx.autoweightapp.core.BaseFragment
import cn.kcrx.autoweightapp.data.AppDatabase
import cn.kcrx.autoweightapp.data.NewCashBoxRecord
import cn.kcrx.autoweightapp.data.NewCashBoxRecordDao
import cn.kcrx.autoweightapp.data.NewCashBoxRepository
import cn.kcrx.autoweightapp.databinding.FragmentDaoTestBinding
import cn.kcrx.autoweightapp.fragment.viewmodes.FragmentDaoTestVM
import com.xuexiang.xaop.annotation.SingleClick
import com.xuexiang.xpage.annotation.Page
import com.xuexiang.xpage.enums.CoreAnim
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.*
import mu.KotlinLogging
import java.text.SimpleDateFormat
import java.util.*
import javax.inject.Inject

@AndroidEntryPoint
@Page(name="数据库测试", anim = CoreAnim.slide)
class FragmengDaoTest:BaseFragment<FragmentDaoTestBinding>() {
    val logger= KotlinLogging.logger {  }
    private lateinit var database: AppDatabase
    private val viewmodel: FragmentDaoTestVM by viewModels()


    override fun viewBindingInflate(
        inflater: LayoutInflater,
        container: ViewGroup
    ): FragmentDaoTestBinding {
      return FragmentDaoTestBinding.inflate(inflater,container,false)
    }

    override fun initViews() {
        viewmodel.newCashBoxRecords.observe(viewLifecycleOwner){
            logger.error { "newCashBoxRecords size=${it.size}" }
//           it.forEach {
//               logger.error { "${it.cashName}-${it.datatime}" }
//               logger.error { "${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(it.datatime.time)}" }
//           }
        }
//         viewmodel.todayRecords.observe(viewLifecycleOwner){
//             logger.error { "todayCashBoxRecords size=${it.size}" }
//             it.forEach{
//                 logger.error { "${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(it.datatime.time)}" }
//             }
//         }
    }

    override fun initListeners() {
        super.initListeners()
        binding!!.btnAppcash.setOnClickListener {
            addCashRec(it)
        }
        binding!!.btnDelAll.setOnClickListener {
            viewmodel.deleteAll()
        }
    }
    override fun onDestroy() {
        super.onDestroy()
    }

//    fun createDb() = runBlocking {
//        database= Room.inMemoryDatabaseBuilder(requireContext(), AppDatabase::class.java).build()
//     //   newCashBoxRecordDao=database.newCashBoxRecordDao()
//    }
    @SingleClick
   fun addCashRec(view:View)= GlobalScope.launch{
        withContext(Dispatchers.IO){
              viewmodel.addCashRec()
        }
   }

}